
package com.tianpengtech.front.mobile.login;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.util.IpKit;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.common.util.SendSmsMsg;
import com.tianpengtech.front.mobile.common.intercepter.MobileRequestInterceptor;
import com.tianpengtech.front.mobile.common.intercepter.TokenCheckInterceptor;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.U;
import com.tianpengtech.front.mobile.common.util.UserUtil;
import com.tianpengtech.front.web.common.controller.BaseController;
import com.tianpengtech.front.web.passport.PassportService;

/**
 * Created by ggd on 2017/7/18 0018.
 */
public class MobileLoginController extends BaseController {

    private static MobileLoginService service = new MobileLoginService();

    private static PassportService passportService = new PassportService();
    
    
    @Clear(TokenCheckInterceptor.class)
    @Before(Tx.class)
    public void login(){
    	String loginIp = IpKit.getRealIp(getRequest());
        String mobile=getPara("mobile");
        String password= getPara("password");
        R r = service.login(mobile, password, loginIp, "ios");
        renderJson(r);
    }
    
    /**
     * 登录
     */
    @Clear(TokenCheckInterceptor.class)
    @Before(Tx.class)
    public void register(){
        String mobile=getPara("mobile");
        String code= getPara("authCode","0");
        String password = getPara("password");
        String confirmPass = getPara("confirmPass");
        if (!password.equals(confirmPass)){
            renderJson(R.fail("两次密码输入不一致"));
            return;
        }
        String loginIp = IpKit.getRealIp(getRequest());
        R r = null;
        try {
            r = service.register(mobile,password,Integer.parseInt(code),loginIp,"ios");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(R.fail("注册失败"));
            return;
        }
        renderJson(r);
    }

    /**
     * 发送验证码
     */
    @Clear(TokenCheckInterceptor.class)
    public void sendCode(){
        String mobile=getPara("mobile");
        if (!U.isMobile(mobile)){
            renderJson(R.fail("手机号码格式不正确"));
            return;
        }
        if (PassportService.me.isMobileExists(mobile,User.USER_TYPE_FAIR)) {
        	R.fail("手机号码已被注册，请更换一个手机号码");
            return;
        }
        
        String ip = IpKit.getRealIp(getRequest());
        Ret ret=passportService.sendAuthCode(mobile,ip,SendSmsMsg.TEMPLATE_REG_SMS);
        if (ret.isOk()){
            R r=R.ok("发送成功");
            r.p("code",ret.get("code"));
            renderJson(r);
        }else{
            renderJson(R.fail("发送失败"));
        }
    }

    
    @Clear(TokenCheckInterceptor.class)
    public void sendForgetPassCode(){
    	String ip = IpKit.getRealIp(getRequest());
        String mobile = getPara("mobile");
        Ret ret=new PassportService().sendAuthCodeByPasswordBack(mobile,ip,User.USER_TYPE_FAIR);
//        Ret ret=Ret.ok("code", "123456");
        if(ret.isOk()){
        	R r=R.ok("发送成功");
            r.p("authcode",ret.get("code"));
            renderJson(r);
        }else{
            renderJson(R.fail(ret.getStr("msg")));
        }
    }
    
    
    
    
    /**
     * 退出登录
     */
    public void logout(){
        String token = getPara("token");
        if (StrKit.isBlank(token)){
            renderJson(R.fail("token不能为空"));
            return;
        }
        R r=service.logout(token);
        renderJson(r);
    }


    /**
     * 邀请注册链接 跳转页面
     */
    @Clear({TokenCheckInterceptor.class,MobileRequestInterceptor.class})
    public void invite(){
    	renderText("根据产品业务设定，当前邀请注册功能暂时不对外开放，感谢您的关注！！！");
    	return;
//        String code=getPara("code");
//        setAttr("code",code);
//        render("invite-reg.html");
    }

    /**
     * 邀请注册 同所有api接口
     */
    @Clear({TokenCheckInterceptor.class})
    public void inviteReg(){
        String code=getPara("code");
        String mobile=getPara("mobile");
        String authCode=getPara("authCode");
        if (StrKit.isBlank(code)){
            renderJson(R.fail("非法邀请链接"));
            return;
        }
        if (StrKit.isBlank(mobile)){
            renderJson(R.fail("手机号码不能为空"));
            return;
        }
        if (StrKit.isBlank(authCode)){
            renderJson(R.fail("验证码不能为空"));
            return;
        }
        if (!U.isInteger(authCode)){
            renderJson(R.fail("验证码格式不正确"));
            return;
        }
        String regIp = IpKit.getRealIp(getRequest());
        try {
            R r=service.inviteReg(code,mobile,Integer.parseInt(authCode),regIp);
            renderJson(r);
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(R.fail("注册失败"));
            return;
        }
    }
    
    public void inviteLink(){
    	User user= UserUtil.getCurrentUser(getPara("token"),"ios");
    	Map<String,Object> result = new HashMap<String,Object>();
    	result.put("url", "http://www.ele-young.com/api/invite?code="+user.getStr(User.CODE));
    	renderJson(JsonUtil.printObject(result));
    }
    
    @Clear(TokenCheckInterceptor.class)
    @Before(Tx.class)
    public void forgetPassWord(){
    	String mobile=getPara("mobile");
        String code= getPara("authCode","0");
        String password = getPara("password");
        String confirmPass = getPara("confirmPass");
        
        if (StrKit.isBlank(code)){
            renderJson(R.fail("验证码不能为空"));
            return;
        }
        if (!U.isInteger(code)){
            renderJson(R.fail("验证码格式不正确"));
            return;
        }
        
        if (!password.equals(confirmPass)){
            renderJson(R.fail("两次密码输入不一致"));
            return;
        }
        R r = null;
        Ret ret = passportService.passwordBack(mobile, password, User.USER_TYPE_FAIR);
        if(ret.isOk()){
        	renderJson(R.ok("成功重置密码，点击确定为您返回登录窗口！"));
        }else{
        	renderJson(R.fail(ret.getStr("msg")));
        }
        
    }
    
    public void upateVersion(){
    	R r = R.ok();
    	r.put("showUpdate", true);
    	r.put("showInvite", false);
    	renderJson(r);
    }
}
